package com.probiz.estore.product.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.ArrayUtils;

import com.probiz.estore.common.model.product.ProductSkuOptionValue;
import com.probiz.estore.core.service.impl.GenericManagerImpl;
import com.probiz.estore.product.dao.ProductSkuOptionValueDao;
import com.probiz.estore.product.service.ProductSkuOptionValueManager;


/**
 * Manager implementation for ProductSkuOptionValue, responsible for business processing, and communicate between web and persistence layer.
 */
public class ProductSkuOptionValueManagerImpl extends GenericManagerImpl<ProductSkuOptionValue> implements ProductSkuOptionValueManager {

	private ProductSkuOptionValueDao productSkuOptionValueDao = null;

	/**
	 * @param productSkuOptionValueDao
	 *            the productSkuOptionValueDao to set
	 */
	public void setProductSkuOptionValueDao(ProductSkuOptionValueDao productSkuOptionValueDao) {
		this.productSkuOptionValueDao = productSkuOptionValueDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#init()
	 */
	@Override
	protected void initManager() {
		dao = productSkuOptionValueDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#preDelete(java.lang.Object)
	 */
	@Override
	protected void preDelete(ProductSkuOptionValue entity) {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#preSave(java.lang.Object)
	 */
	@Override
	protected void preSave(ProductSkuOptionValue entity) {

	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.product.service.ProductSkuOptionValueManager#saveProductSkuOptionValueFromArrays(java.lang.String, java.lang.String[])
	 */
	public void saveProductSkuOptionValueFromArrays(String skuId, String[] skuOptionValueIds){
		//删除单品与规格值关联
		List<String> skuOptionValueIdList=new ArrayList<String>(); //必须用这种方法，不能用ArrayUtil.asList方法，否则后面remove会出错，因为arraylist支持remove。
		for(String skuOptionValueId : skuOptionValueIds){
			skuOptionValueIdList.add(skuOptionValueId);
		}
		List<ProductSkuOptionValue> formerRelationList=productSkuOptionValueDao.getBySkuId(Integer.valueOf(skuId));//原来的关联
		for (ProductSkuOptionValue formerRelation : formerRelationList) {
			if(!skuOptionValueIdList.contains(formerRelation.getSkuOptionValueId().toString())){
				deleteById(formerRelation.getProductSkuOptionValueId());
			}else{
				skuOptionValueIdList.remove(formerRelation.getSkuOptionValueId().toString());
			}	
		}
		//增加关联
		for (String skuOptionValueId : skuOptionValueIdList) {
			ProductSkuOptionValue productSkuOptionValue = new ProductSkuOptionValue();
			productSkuOptionValue.setProductSkuId(Integer.parseInt(skuId));
			productSkuOptionValue.setSkuOptionValueId(Integer.parseInt(skuOptionValueId));
			save(productSkuOptionValue);
		}
		
	}
	
}
